package dev.entao.core.slices

import dev.entao.core.*
import dev.entao.log.logd
import kotlin.reflect.KClass

object TickSlice : HttpSlice {

    override fun beforeRouter(context: HttpContext, cls: KClass<*>, action: HttpAction) {
        context.setAttr("TimeSlice.start", System.currentTimeMillis())
    }

    override fun afterRouter(context: HttpContext, cls: KClass<*>, action: HttpAction) {
        val a: Long = context.getAttr("TimeSlice.start") as? Long ?: return
        val delta = System.currentTimeMillis() - a
        logd("请求${cls.pageName}.${action.actionName}用时: ${delta.formatedSeconds} 秒")
    }
}

val Long.formatedSeconds: String
    get() {
        return String.format("%d.%03d", this / 1000, this % 1000)
    }
